package de.parser.amazon;

import java.net.MalformedURLException;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;

import de.dao.AbstractTest;
import de.preiscrawler.AlleAnbieter;
import de.preiscrawler.parser.amazon.AmazonParser;
import de.preisrobot.entity.Artikel;
import de.preisrobot.entity.CrawlerLinkUrl;
import de.preisrobot.service.GrabberService;
import de.preisrobot.service.helper.UrlHelper;

public class TEstAmazonParser extends AbstractTest {

    @Resource(name = "amazonGrabberService")
    private GrabberService amazonGrabberService;

    @Test
    public void testAmazonParser() throws Exception {
        final String url =
                "http://www.amazon.de/s/ref=amb_link_157495247_19/280-2409696-5136667?ie=UTF8&rh=n%3A427954031%2Ck%3AMultimedia&page=2&pf_rd_m=A3JWKAKR8XB7XF&pf_rd_s=browse&pf_rd_r=1VTS80YBRQY69B21G9JY&pf_rd_t=101&pf_rd_p=248882167&pf_rd_i=514699031";

        final AmazonParser p = new AmazonParser();
        final List<Artikel> parse = p.parse(url);

        for (final Artikel amazonArtikel : parse) {
            final String artikelNr = amazonArtikel.getArtikelNr();
            System.out.println(artikelNr);
        }
    }

    @Test
    public void testGrepAmazon() throws InterruptedException, MalformedURLException {
        String sourceUrlString =
                "http://www.amazon.de/b/ref=amb_link_157565367_3?ie=UTF8&node=430127031&pf_rd_m=A3JWKAKR8XB7XF&pf_rd_s=browse&pf_rd_r=035D737CNE93CD1WRA8Q&pf_rd_t=101&pf_rd_p=223136027&pf_rd_i=514840031#/ref=sr_pg_3?rh=n%3A340843031%2Cn%3A!340844031%2Cn%3A427955031%2Cn%3A430127031&page=6&ie=UTF8&qid=1312035018";

        int i = 10;
        while (i-- > 0) {

            final CrawlerLinkUrl li = new CrawlerLinkUrl();
            li.setAnbieterId(AlleAnbieter.AMAZON);
            li.setLink(sourceUrlString);
            System.out.println("grabbing url " + sourceUrlString);

            final List<Artikel> grapUrl = amazonGrabberService.grapUrl(li);

            for (final Artikel artikel : grapUrl) {
                System.out.println("Got Artikel " + artikel);
            }

            Thread.sleep(1000);

            final UrlHelper u = new UrlHelper(sourceUrlString);
            final String parameterValue = u.getParameterValue("page");
            int parseInt = Integer.parseInt(parameterValue);

            final String string = u.setParameter("page", "" + (++parseInt));
            sourceUrlString = string;

        }
    }
}
